package com.example.springbootdemo2.controller;

import com.example.springbootdemo2.service.ExcelService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
@Slf4j
@RestController
@RequestMapping("/Excel")
public class ExcelConteroller {

    @Autowired
    private ExcelService excelService;

    /**
     * 方法名：
     * 功能：导出excel
     * 描述：文件格式为xls或xlsx
     */
    @GetMapping("/export/excel")
    public String exportExcel(HttpServletResponse response,String fileName,Integer pageNum,Integer pageSize){

        if(fileName==null||"".equals(fileName)) {
            return "文件名不能为空";
        }else{
            //开始时间
            long begin=System.currentTimeMillis();
            if(fileName.endsWith("xls")||fileName.endsWith("xlsx")){
                Boolean isOk = excelService.exportExcel(response, fileName, pageNum, pageSize);
                if (isOk){
                    //结束时间
                    long end=System.currentTimeMillis();
                    System.out.println((double) (end-begin)/1000+"s");
                    return "导出成功！";
                }else {
                    return "导出失败";
                }
            }
            return "文件格式有误！";
        }
    }
    /**
     * 方法名：
     * 功能：导入excel
     * 描述：文件格式为xls或xlsx
     */

    @GetMapping("/import/excel")
    public String importExcel(String fileName){
        if (fileName == null && "".equals(fileName)) {
            return "文件名不能为空！";
        } else {
            if (fileName.endsWith("xls") || fileName.endsWith("xlsx")) {
                Boolean isOk = excelService.importExcel(fileName);
                if (isOk) {
                    return "导入成功！";
                } else {
                    return "导入失败！";
                }
            }
            return "文件格式错误！";
        }
    }
}
